<?php
class App_Module_Admin_Component_Structure_Ctrl_Http_Web extends App_Module_Admin_Component_Structure_Ctrl_Http
{
    private $_pageParamsFieldName = 'page_params';
    private $_moduleParamsFieldName = 'module_params';

    protected function PrimaryActionInit ($basicActionName)
    {
        Mozg::Layout('admin')->SetMenuActiveElement('structure/list');
        Mozg::SubAction()->Set('Left Sidebar', array($this, 'tree'));
    }
    /**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
        $this->listPr();
    }

    public function tree()
    {
        return $this->GetTpl('Web')->CatList($this->_cid, $this->GetExpandedElements());
    }

    public function listPrAction ()
    {
        Mozg::Response('http')->SetBody($this->GetTpl('List')->GetList($this->_cid));
    }

    public function addPrAction ($category = null)
    {
        if (!$category)
        {
        	if (!$this->_subCat)
        	{
        		$category = $this->_structMapper->GetEntity();
        	}
        	else
        	{
        		$category = $this->_structMapper->GetOne(array('id' => $this->_subCat));
        	}
        }

        $t = $this->GetTpl('Editform');
        Mozg::Response('http')->SetBody($t->GetForm($this->_cid, $category));

        if (!$category->GetId())
        {
            Mozg::Layout('admin')->SetMenuActiveElement('structure/add');
        }
    }

    public function savePrAction ()
    {
      	if (!$this->_subCat) $category = $this->_structMapper->GetEntity();
      	else $category = $this->_structMapper->GetOne(array('id' => $this->_subCat));

   		$data = (array)Mozg::Request('http')->getPost('category');

   		$this->GetTpl('Editform')->AddPostData($category, $data);

        $pageParams = $category->GetPageParams();
        $this->GetTpl('Pageparams')->SetPostData($pageParams, (array)Mozg::Request('http')->getPost($this->_pageParamsFieldName));

        $moduleParams = $category->GetModuleParams();
        $this->GetTpl('Moduleparams')->SetPostData($moduleParams, (array)Mozg::Request('http')->getPost($this->_moduleParamsFieldName));

		if (!$category->GetId())
        {
        	$category->SetParentId($this->_cid);
        }

        if ($this->_structMapper->Save($category))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('CATEGORY_SAVED'));

			Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'list', 'cat' => $this->_cid));
		}
        else
        {
			if (!$category->Validator()->GetState())
        	{
        		Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
        	}
        	else
        	{
	            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('DATA_NOT_SAVED'), MESSAGE_ERROR);
        	}

            $this->addPr($category);
        }
    }

    public function delPrAction ()
    {
    	$item = $this->_structMapper->GetOne(array('id' => $this->_subCat));

    	if (!$item)
    	{
			Mozg::Lib('messanger')->createMessage('CATEGORY_NOT_FOUND', MESSAGE_ERROR);
    	}
		elseif (!$item->GetBlocked() || Mozg::User()->IsSuperUser())
        {
	        if (!$this->_structMapper->Delete(array('id' => $item->GetId())))
	        {
	            Mozg::Lib('messanger')->createMessage('UNABLE_DELETE_CATEGORY', MESSAGE_ERROR);
	        }
	        else
	        {
	            Mozg::Lib('messanger')->createMessage('CATEGORY_DELETED');
	        }
        }
        else
        {
	        Mozg::Lib('messanger')->createMessage('UNABLE_DELETE_CATEGORY', MESSAGE_ERROR);
        }

        if ($this->_subCat != $this->_cid) $redirectCid = $this->_cid;
        else $redirectCid = $item->GetParentId();

        Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'list', 'cat' => $redirectCid));
    }

    public function editbodyPrAction()
    {
       	if (!$this->_subCat)
       	{
       		Mozg_Router::Get('http')->Redirect('module=admin&ctrl=structure&action=add&cat=' .$this->_cid);
       	}
       	else $category = $this->_structMapper->GetOne(array('id' => $this->_subCat));

        $t = $this->GetTpl('Editbodyform');
        Mozg::Response('http')->SetBody($t->GetForm($this->_cid, $category));

        $baseUrlParams = array(
        						'module' => 'admin',
        						'ctrl' => $this->GetName(),
        						'action' => 'mediaadd',
        						'cat' => $this->_cid,
        						'subcat' => $this->_subCat
        					);

        Mozg::SubAction()->Set('Left Sidebar', array($this->GetCtrl('media'), 'mediablock'), array('baseUrl' => $baseUrlParams, 'params' => array('contentButton' => true)));
    }

    public function mediaaddPrAction()
    {
    	$this->editbodyPr();
    }

    public function savebodyPrAction ()
    {
       	if (!$this->_subCat)
       	{
       		Mozg_Router::Get('http')->Redirect('module=admin&ctrl=structure&action=add&cat=' .$this->_cid);
       	}
       	else $elementObj = $this->_structMapper->GetOne(array('id' => $this->_subCat));

   		$this->GetTpl('Editbodyform')->AddPostData($elementObj, (array)Mozg::Request('http')->getPost('category'));

   		if (!$this->_structMapper->Save($elementObj))
        {
            Mozg::Lib('messanger')->createMessage('CONTENT_NOT_UPDATED', MESSAGE_ERROR);
        }
        else
        {
            Mozg::Lib('messanger')->createMessage('CONTENT_UPDATED');
        }

        Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'structure', 'action' => 'list', 'cat' => $this->_cid));
    }
}